package com.gitee.starblues.grape.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.starblues.grape.core.security.UserService;
import com.gitee.starblues.grape.core.security.model.MenuTree;
import com.gitee.starblues.grape.core.service.ChailvProviderProductService;
import com.gitee.starblues.grape.repository.databases.entity.ChailvProviderProduct;
import com.gitee.starblues.grape.repository.databases.entity.RoleMenu;
import com.gitee.starblues.grape.repository.databases.mapper.ChailvProviderProductMapper;
import com.gitee.starblues.grape.rest.common.BaseResource;
import com.gitee.starblues.grape.rest.common.Result;
import com.gitee.starblues.grape.rest.common.enums.ApiEnum;
import com.gitee.starblues.grape.rest.common.vo.RoleMenuVo;
import com.gitee.starblues.grape.rest.model.param.user.UserAddParam;
import com.gitee.starblues.grape.rest.model.vo.ProviderProductVO;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.gitee.starblues.grape.rest.common.Result.*;
import static com.gitee.starblues.grape.rest.common.ResultUtils.errorLog;

/**
 * @author l.zhu3
 * @date 2025/1/25 12:24
 */
@RestController
@RequestMapping(value = "/open")
@AllArgsConstructor
@Api(tags = {"开放接口"})
@Slf4j
public class OpenAPIController extends BaseResource {

    private final UserService userService;
    private final ChailvProviderProductService chailvProviderProductService;

    @GetMapping("/product-list")
    @ApiOperation("查询返利列表")
    public Result<List<ProviderProductVO>> getProductList(@RequestParam("productCode") String productCode,@RequestParam("channelType") String channelType ) {
        try {
            return success(ApiEnum.GET_SUCCESS, chailvProviderProductService.getProductList(productCode, channelType));
        } catch (Exception e) {
            errorLog(log, e, "查询返利列表失败");
            return failure(ApiEnum.GET_SUCCESS, e);
        }
    }

    @PostMapping("/register")
    @ApiOperation("添加用户")
    public Result<String> add(@RequestBody @Valid UserAddParam param){
        try {
            Set<String> roleIds = new HashSet<>();
            roleIds.add("06701657d9513f8819040687e9c4979a");
            param.setRoleIds(roleIds);
            param.setStatus(1);
            userService.addUser(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(log, e, "新增用户 '{}' 信息失败 {}", param.getUsername(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增用户失败", e);
        }
    }
}
